package com.michael.demos.base.java.queue;

import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;

/**
 * 类功能描述:
 * <pre>
 *   优先队列使用示例
 * </pre>
 *
 * @author Michael
 * @version 1.0
 * @date 2020/9/15 9:37
 */
public class PriorityQueueDemo {

    public static void main(String[] args) {

        //优先队列使用示例
        int initSize = 20;
        Queue<Customer> queue = new PriorityQueue<>(initSize);
        Random random = new Random();
        for (int i = 0; i < initSize; i++) {
            int money = random.nextInt(100);
            queue.add(new Customer(money, "客户" + i));
        }

        System.out.println("谁钱多谁优先");
        while (!queue.isEmpty()) {
            Customer customer = queue.remove();
            System.out.println("优先处理\t" + customer.getName() + "\t因为他的money为:\t" + customer.getMoney() + "￥");
        }
    }
}

class Customer implements Comparable<Customer> {

    private int money;

    private String name;

    public Customer(int money, String name) {
        this.money = money;
        this.name = name;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int compareTo(Customer another) {
        return Integer.compare(another.money, this.money);
    }

    @Override
    public String toString() {
        return "Customer{" +
               "money=" + money +
               '}';
    }
}
